package com.smzd.ccms.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smzd.ccms.domain.Channel;
import com.smzd.ccms.mappers.ChannelMapper;
import com.smzd.core.dao.AbstractDao;
import com.smzd.core.mappers.IMapper;
import com.smzd.framework.utils.db.RPage;

@Component
public class ChannelDao extends AbstractDao<Channel> {
	@Autowired
	private ChannelMapper mapper;

	@Override
	protected IMapper<Channel> getMapper() {
		return mapper;
	}

	/**
	 * 系统初始化的时候获取所有的有效栏目。
	 * 
	 * @return
	 */
	public List<Channel> getAllChannels() {
		return mapper.getAllChannels();
	}

	/**
	 * 展示站点栏目树的时候需要获取所有栏目及其父子关系，包括owner。因此单列
	 * 
	 * @return
	 */
	public List<Channel> getAllChannelsWithParent() {
		return mapper.getAllChannelsWithParent();
	}

	public List<Channel> getAllChannelsByOwnerId(int meetingId) {
		return mapper.getAllChannelsByOwnerId(meetingId);
	}

	public List<Channel> getSubChannelsByParentId(int chId) {
		return mapper.getSubChannelsByParentId(chId);
	}

	public List<Channel> getSublingChannelsByPath(String sPath, String cPath) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sPath", sPath);
		params.put("cPath", cPath);
		return mapper.getSublingChannelsByPath(params);
	}

	public RPage<Channel> getTemplateChannels(RPage<Channel> page) {
		List<Channel> elems = mapper.getTemplateChannels(page);
		page.setElems(elems);
		return page;
	}

	public void updateSequence(Channel ch) {
		mapper.updateSequence(ch);
	}

	public Channel getChannelByPath(String siteStr, String channelStr) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sPath", siteStr);
		params.put("cPath", channelStr);
		return mapper.getChannelByPath(params);
	}

	public void updateType(Channel channel) {
	    mapper.updateType(channel);
	    
    }
	public Channel getChannelByPathId(String siteStr, int chId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sPath", siteStr);
		params.put("chId", chId);
		return mapper.getChannelByPathId(params);
    }

	public Channel getBreadcrumbChannelsByChId(int chId) {
	    return mapper.getBreadcrumbChannelsByChId(chId);
    }

	public Channel getBreadcrumbChannelsByPath(String siteStr, String channelStr) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sPath", siteStr);
		params.put("cPath", channelStr);
		return mapper.getBreadcrumbChannelsByPath(params);
    }

    public RPage<Channel> getChannelsWithLastArticleByPage(RPage<Channel> page) {
        preparePage(page);
        List<Channel> elems = mapper.getChannelsWithLastArticleByPage(page);
        for (Channel ch : elems) {
            if ("public".equalsIgnoreCase(ch.getPath())) {
                page.setElem(ch);
                break;
            }
        }
        elems.remove(page.getElem());
        page.setElems(elems);
        int totalCount = mapper.getChannelsCountByPage(page);
        page.setTotalCount(totalCount - 1);
        completePage(page);
        return page;
    }

}